<template>
  <view>
    <!-- 选择收货地址区域 -->
    <view class="choose-address-box" v-if="JSON.stringify(address) === '{}'">
      <button type='primary' size='mini' @click="chooseAddress">请选择收货地址+</button>
    </view>
    <!-- 收货地址详情区域 -->
    <view class="address-info-box" v-else @click='chooseAddress'>
      <view class="address_info_row1">

        <view class="row1_left">
          <view class="username">收货人：<text>{{address.userName}}</text></view>
        </view>

        <view class="row1_right">
          <view class="phone">电话：<text>{{address.telNumber}}</text></view>
          <uni-icons type='right' size='17'></uni-icons>
        </view>

      </view>
      <view class="address_info_row2">
        <view class="row2_left">收货地址:</view>
        <view class="row_right">{{ addStr }}</view>
      </view>
    </view>
  </view>
</template>

<script>
  import {
    mapState,
    mapGetters,
    mapMutations
  } from 'vuex'

  export default {
    name: "my-address",
    data() {
      return {
        // address: {}
      };
    },
    computed: {
      ...mapState('m_user', ['address']),
      ...mapGetters('m_user', ['addStr']),
    },
    methods: {
      ...mapMutations('m_user', ['updateAddress']),
      async chooseAddress() {
        // 1. 调用小程序提供的 chooseAddress() 方法，即可使用选择收货地址的功能 
        // 返回值是一个数组：第 1 项为错误对象；第 2 项为成功之后的收货地址对象
        const [err, succ] = await uni.chooseAddress().catch(err => err)
        // console.log(err)
        // 如果用户成功的选择了收货地址
        if (err === null && succ.errMsg === 'chooseAddress:ok') {
          // this.address = succ
          this.updateAddress(succ)
        }
        // 用户没有授权
        if (err && (err.errMsg === 'chooseAddress:fail auth deny' || err.errMsg ===
            'chooseAddress:fail authorize no response')) {
          this.reAuth() // 调用 this.reAuth() 方法，向用户重新发起授权申请 
        }
      },
    },
    async reAuth() {
      // 提示用户对地址进行授权
      const [err2, result] = await uni.showModal({
        content: '检测到您没打开地址权限，是否去设置打开？',
        confirmText: "确认",
        cancelText: "取消",
      })
      // 如果弹框异常,直接退出
      if (err2) return
      // 如果点击了取消按钮
      if (result.cancel) return uni.$showMsg('您取消了地址授权!')
      // 如果用户点击了 “确认” 按钮，则调用 uni.openSetting() 方法进入授权页面， 让用户重新进行授权
      if (result.confirm) return uni.openSetting({
        // 授权结束，需要对授权的结果做进一步判断
        success: settingResult => {
          // 地址授权的值等于 true，提示用户 “授权成功”
          if (settingResult.authSetting['scope.address']) return uni.$showMsg('授权成功！请选择地址')

          // 地址授权的值等于 false，提示用户 “您取消了地址授权”
          if (!settingResult.authSetting['scope.address']) return uni.$showMsg('您取消了地址授权!')
        }
      })
    },
  }

</script>

<style lang="scss">
  .choose-address-box {
    height: 90px;
    display: flex;
    justify-content: center;
    align-items: center;
  }

  .address-info-box {
    height: 90px;
    padding: 0 5px;
    display: flex;
    flex-direction: column;
    justify-content: center;

    .address_info_row1 {
      display: flex;
      justify-content: space-between;

      .row1_left {
        .username {
          font-size: 12px;
        }
      }

      .row1_right {
        display: flex;
        align-items: center;

        .phone {
          font-size: 12px;
          margin-right: 5px;
        }
      }
    }

    .address_info_row2 {
      margin-top: 10px;
      display: flex;
      // justify-content: space-between;
      font-size: 12px;
      align-items: center;

      .row2_left {
        white-space: nowrap;
      }

      .row_right {
        margin-left: 5px
      }
    }

  }
</style>
